﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Photoshop;

namespace SmallERP.Host
{
    public class PSApplition
    {
        private _Application _application;
        private _JPEGSaveOptions jpgSaveOptions;
        public PSApplition()
        {

            //Type indAppType = Type.GetTypeFromProgID("Photoshop.Application");
            //_application = (_Application)Activator.CreateInstance(indAppType, true);
            FindApp();
            Type inSaveOptione = Type.GetTypeFromProgID("Photoshop.JPEGSaveOptions");
             jpgSaveOptions = (JPEGSaveOptions)Activator.CreateInstance(inSaveOptione, true);
            //_application.Visible = false;
            _application.DisplayDialogs = PsDialogModes.psDisplayNoDialogs;
           


        }

        private void FindApp()
        {
            if (_application == null)
            {
                Type indAppType = Type.GetTypeFromProgID("Photoshop.Application");
                _application = (_Application) Activator.CreateInstance(indAppType, true);
            }
        }

        public void ConvertRGBToCMYK(string srcPath, string toPath)
        {
            FindApp();
            var document = _application.Open(srcPath);

            document.ChangeMode(PsChangeMode.psConvertToCMYK);
            document.BitsPerChannel = PsBitsPerChannelType.psDocument8Bits;
            var saveOptions = new JPEGSaveOptions();

            document.ConvertProfile(@"Japan Color 2001 Coated", PsIntent.psRelativeColorimetric, true, true);

            saveOptions.EmbedColorProfile = true;
            document.SaveAs(toPath, jpgSaveOptions);
            document.Close();
        }
        public void ConvertRGBToSRgb1966(string srcPath, string toPath)
        {
             FindApp();
            var document = _application.Open(srcPath);

            document.ChangeMode(PsChangeMode.psConvertToRGB);
            //document.BitsPerChannel = PsBitsPerChannelType.psDocument8Bits;
            var saveOptions = new JPEGSaveOptions();

            document.ConvertProfile(@"sRGB IEC61966-2.1", PsIntent.psRelativeColorimetric, true, true);

            saveOptions.EmbedColorProfile = true;
            document.SaveAs(toPath, jpgSaveOptions);
            document.Close();
        }

        public void RotatePic(string srcPath, string toPath,int Angle)
        {
            try
            {
                FindApp();
         
           Document document = _application.Open(srcPath);
           document.RotateCanvas(Angle);
           
           jpgSaveOptions.EmbedColorProfile = true;
           jpgSaveOptions.FormatOptions = PsFormatOptionsType.psStandardBaseline;
           jpgSaveOptions.Matte = PsMatteType.psNoMatte;
           jpgSaveOptions.Quality = 12;


            document.SaveAs(toPath, jpgSaveOptions);
           // document.Save();
           document.Close();
            }
            catch (Exception exception)
            {

                throw new Exception("ps旋转失败");
            }
        }

        public string AddICC(string Iccfname, string srcPath, string toPath)
        {
            FindApp();
            var document = _application.Open(srcPath);

            document.ChangeMode(PsChangeMode.psConvertToRGB);
            document.BitsPerChannel = PsBitsPerChannelType.psDocument8Bits;
            var saveOptions = new JPEGSaveOptions();

            document.ConvertProfile(Iccfname, PsIntent.psRelativeColorimetric, true, true);

            saveOptions.EmbedColorProfile = true;
            document.SaveAs(toPath, jpgSaveOptions);
            document.Close();
            return toPath;
        }
    }
}
